package com.hegongda.servlet;

import com.hegongda.bean.*;
import com.hegongda.service.OrderService;
import com.hegongda.utils.BeanFactory;
import net.sf.json.JSONObject;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;

@WebServlet(value = "/orderServlet")
public class OrderServlet extends BaseServlet {
    private OrderService os = BeanFactory.newInstance(OrderService.class);
    public void submitOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取出session中的购物车
        Cart cart = (Cart) request.getSession().getAttribute("carts");

        //判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            request.setAttribute("error","未登录");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
            return;
        }
        //判断购物车是否为空
        if (cart.getItemMap().size() == 0) {
            request.setAttribute("error","购物车空");
            request.getRequestDispatcher("/view/cart/list.jsp").forward(request,response);
            return;
        }
        String oid = UUID.randomUUID().toString().replace("-","");
        os.addOrders(cart,user,oid);

        Set<String> strings = cart.getItemMap().keySet();
        for (String string : strings) {
            Cart cart1 = (Cart)request.getSession().getAttribute("cart");
            cart1.getItemMap().remove(string);
            cart1.setTotal(cart1.getTotal()-cart.getItemMap().get(string).getSubTotal());
        }

        cart.setTotal(0);
        request.setAttribute("oid",oid);
        getOneOrder(request,response);
    }

    public void getAllOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            request.getRequestDispatcher("/login.jsp").forward(request,response);
            return;
        }
        String pagenum = request.getParameter("pagenum");
        if(pagenum==null){
            pagenum="1";
        }
        PageBean<Orders> allOrder = os.getAllOrder(user.getUid(), 3, Integer.parseInt(pagenum));
        request.setAttribute("pb",allOrder);
        request.getRequestDispatcher("/view/order/list.jsp").forward(request,response);
    }
    public void getOneOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            request.setAttribute("error","未登录");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
            return;
        }
        String oid = request.getParameter("oid");
        if(request.getAttribute("oid")!=null) {
            oid = (String) request.getAttribute("oid");
        }
        OrderService os = BeanFactory.newInstance(OrderService.class);
        Orders oneOrder = os.getOneOrder(oid);
        request.setAttribute("order",oneOrder);
        request.getRequestDispatcher("/view/order/info.jsp").forward(request,response);
    }

    public void pay(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //接受参数
        String address = request.getParameter("address");
        String name = request.getParameter("name");
        String telephone = request.getParameter("telephone");
        String oid = request.getParameter("oid");
        //修改订单信息
        os.updateOrder(name, address, telephone, oid);
        os.updateOrderState(oid,Constr.ORDER_YIFUKUAN);
        getAllOrder(request,response);
    }

    public void userDelOrder(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String oid = request.getParameter("oid");
        //修改订单信息
        os.updateOrderState(oid,Constr.ORDER_YONGHUSC);
        getAllOrder(request,response);
    }

    public void userConfirmOrder(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String oid = request.getParameter("oid");
        //修改订单信息
        os.updateOrderState(oid,Constr.ORDER_YIWANC);
        getAllOrder(request,response);
    }

    public void delOrder(HttpServletRequest request,HttpServletResponse response) throws Exception {
        User user = (User)request.getSession().getAttribute("user");
        if(user != null) {
            String oid = request.getParameter("oid");
            int i = os.delOrder(oid);
            if (i <= 0) {
                request.setAttribute("error", "订单取消失败");
            }
        }
        getAllOrder(request,response);
    }

}
